include std/dll.e
include std/machine.e
include std/convert.e

include core.e
include wrap_helpers.e

constant
	hQtScriptEngineCreate = define_c_func(hEuQt, "qt_scriptengine_create", {}, P),
	hQtScriptEngineEvalInt = define_c_func(hEuQt, "qt_scriptengine_eval_integer", {P,P}, I),
	hQtScriptEngineEvalFloat = define_c_func(hEuQt, "qt_scriptengine_eval_float", {P,P}, F),
	hQtScriptEngineEvalString = define_c_func(hEuQt, "qt_scriptengine_eval_string", {P,P}, P),
	hQtScriptEngineEvalQScript = define_c_func(hEuQt, "qt_scriptengine_eval_script", {P,P}, P),
	$

--****
-- allows you to initilize a QScript (javascript) engine, you can evaluate
-- scripts and preload the engine with functions that can be called or
-- connected to signals in a mor dynamic way than is usually possible in euphoria.
-- the usual security warnings apply to running user scripts, that is they can
-- easily crash or hang your application. some debugging output is provided
-- for syntax or other errors in scripts.
--<<LEVELTOC depth=2>>

--**
--need another name. createEngine? 
public function create()
	return c_func(hQtScriptEngineCreate, {})
end function

--****
--===evalType
--all the evalType will return that type
--there will be an uncaucht exception routine
--and a way to handle errors with a callback
--as well as connecting events to scripts and vice versa
--currently evalForString prints any uncaught exceptions to the console
--try that if you are having a problem getting a valid result
--
--note: you can no doubt easily hang your app with a runaway script
-- will later have timeout settings and ability to attach script debugging
-- 
-- see the Qt QScript docs for more details

--**
--evaluate the script and return an integer result
-- <eucode>
-- ? evalForInteger(engine, "4+5")
-- --9
-- </eucode>
--
public function evalForInteger(atom engine, sequence script)
	atom pScript = allocate_string(script,1)
	integer result = c_func(hQtScriptEngineEvalInt, {engine, pScript})
	--~ free(pScript)
	return result
end function

--**
--evaluate the script and return a float/real result
-- <eucode>
-- ? evalForReal(engine, "4.5+5.2")
-- --9.69999981  --no it's not broken. real numbers have to be formatted
-- printf(1,"%4.1f",{evalForReal(engine, "4.5+5.2")} )
-- --9.7  --formatted to one decimal place using euphoria printf
-- </eucode>
--
public function evalForReal(atom engine, sequence script)
	atom pScript = allocate_string(script,1)
	atom result = c_func(hQtScriptEngineEvalFloat, {engine, pScript})
	return result
end function

--**
--evaluate the script and return a string result
-- <eucode>
-- printf(1,"%s\n",{ evalForString(engine, "4.5+5.21") })
-- --"9.71"  evalForString rounds real numbers
--</eucode>
--if debugging is enabled in the shared lib and a console is available
--any uncaught exceptions will be output to the console
-- <eucode>
-- printf(1,"%s\n",{ evalForString(engine, "4.5+x") })
-- --  "ReferenceError: x is not defined"
-- </eucode>
-- should see something like 
--uncaught exception at line 1 : "ReferenceError: x is not defined"

public function evalForString(atom engine, sequence script)
	atom pScript = allocate_string(script,1)
	atom result = c_func(hQtScriptEngineEvalString, {engine, pScript})
	return peek_string(result )
end function

public function evalQScript(atom engine, sequence script)
	atom pScript = allocate_string(script,1)
	atom result = c_func(hQtScriptEngineEvalQScript, {engine, pScript})
	return result
end function

--****
--todo
--change names to something more javascript like less vbscript like
-- change the other eval to catch exception like eval string
--checkSyntax  
--uncaughtexception 
--errorroutine callback
--script timeoutsetting
--script debugger widget

